package com.dd.cloud.user.service.pres.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.PresConstants;
import com.dd.cloud.user.entity.pres.DrugsModel;
import com.dd.cloud.user.entity.pres.DrugsPresAlias;
import com.dd.cloud.user.mapper.pres.DrugsPresAliasMapper;
import com.dd.cloud.user.req.pres.DrugsPresImportReq;
import com.dd.cloud.user.service.pres.IDrugsModelService;
import com.dd.cloud.user.service.pres.IDrugsPresAliasService;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.ExtUtils;
import com.dd.cloud.common.utils.PinYinUtils;
import com.github.houbb.opencc4j.util.ZhConverterUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Service
public class DrugsPresAliasServiceImpl extends ServiceImpl<DrugsPresAliasMapper, DrugsPresAlias> implements IDrugsPresAliasService {
    @Autowired
    private IDrugsModelService drugsModelService;


    /**
     * 导入药材处方数据
     *
     * @param file
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importDrugsPresAliasData(MultipartFile file) {
        List<DrugsPresImportReq> list;
        try {
            if (file == null) {
                throw new ZekeException(400, "请上传文件");
            }
            list = ExtUtils.importExcel(file, 0, 1, DrugsPresImportReq.class);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ZekeException(400, "解析Excel失败");
        }
        Map<String, Integer> map = new HashMap<>(list.size());
        List<DrugsPresAlias> saveOrUpdateBathList = new ArrayList<>(list.size());
        log.info("list:{}", list);
        for (DrugsPresImportReq item : list) {
            if (item.getName() == null) {
                continue;
            }
            log.info("item:{}", item);
            QueryWrapper<DrugsPresAlias> qw = new QueryWrapper<>();
            qw.lambda().eq(DrugsPresAlias::getName, item.getName());
            qw.lambda().eq(DrugsPresAlias::getType, item.getType());
            qw.lambda().eq(DrugsPresAlias::getStatus, PresConstants.DrugsPresStatus.NOMER);
            DrugsPresAlias drugsPres = getOne(qw);
            if (drugsPres == null) {
                String key = item.getName() + "-" + item.getType();
                if (map.get(key) == null) {
                    drugsPres = new DrugsPresAlias();
                    drugsPres.setDrugNum(item.getDrugNum());
                    drugsPres.setName(item.getName());
                    drugsPres.setType(item.getType());
                    drugsPres.setAlias(item.getAlias());
                    drugsPres.setStatus(PresConstants.DrugsPresStatus.NOMER);
                    drugsPres.setCreateDate(LocalDateTime.now());
                    saveOrUpdateBathList.add(drugsPres);
                    map.put(key, saveOrUpdateBathList.size() - 1);
                } else {
                    if (!StringUtils.isBlank(item.getAlias())) {
                        saveOrUpdateBathList.get(map.get(key)).setDrugNum(item.getDrugNum());
                        saveOrUpdateBathList.get(map.get(key)).setAlias(item.getAlias());
                    }
                }
            } else {
                if (!StringUtils.isBlank(item.getAlias())) {
                    drugsPres.setDrugNum(item.getDrugNum());
                    drugsPres.setAlias(item.getAlias());
                    saveOrUpdateBathList.add(drugsPres);
                }
            }
        }
        saveOrUpdateBatch(saveOrUpdateBathList);
    }

    /**
     * 同步药材别名
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void drugsSyncAlias() {
        QueryWrapper<DrugsModel> qw = new QueryWrapper<>();
        qw.lambda().eq(DrugsModel::getStatus, PresConstants.ModelDrugsStatus.NOMER);
        qw.lambda().select(DrugsModel::getId, DrugsModel::getHerbName);
        List<DrugsModel> list = drugsModelService.list(qw);
        List<DrugsModel> updateList = new ArrayList<>(list.size());
        for (DrugsModel item : list) {
            QueryWrapper<DrugsPresAlias> qws = new QueryWrapper<>();
            qws.lambda().eq(DrugsPresAlias::getName, item.getHerbName());
            qws.lambda().eq(DrugsPresAlias::getStatus, PresConstants.DrugsPresStatus.NOMER);
            DrugsPresAlias temp = getOne(qws);
            if (temp != null) {
                item.setAlias(temp.getAlias());
                item.setAliasPinyin(PinYinUtils.chineseToPinyin(temp.getAlias()));
                item.setAliasTraditional(ZhConverterUtil.toTraditional(temp.getAlias()));
                updateList.add(item);
            }
        }
        drugsModelService.updateBatchById(updateList);
    }




}
